<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="right">
			<el-form-item label="学生姓名" prop="name">
				<el-input v-model="form.name" clearable @input="get_mnemonic_code"></el-input>
			</el-form-item>
			<el-form-item label="助记码" prop="mnemonic_code">
				<el-input v-model="form.mnemonic_code" disabled clearable ></el-input>
			</el-form-item>
			<el-form-item label="学生手机号" prop="mobile">
				<el-input v-model="form.mobile" clearable></el-input>
			</el-form-item>
			<el-form-item label="性别">
				<el-select v-model="form.sex" placeholder="请选择">
					<el-option label="保密" :value="0"></el-option>
					<el-option label="男" :value="1"></el-option>
					<el-option label="女" :value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="所属校区" prop="school_code">
				<el-select v-model="form.school_code" filterable style="width: 100%">
					<el-option v-for="item in schools" :key="item.code" :label="item.name" :value="item.code"/>
				</el-select>
			</el-form-item>
			<el-form-item label="生日" prop="birthday">
				<el-date-picker v-model="form.birthday" type="date" value-format="YYYY-MM-DD" placeholder="选择日期时间"></el-date-picker>
			</el-form-item>
			<el-form-item label="家长手机号" prop="parent_mobile">
				<el-input v-model="form.parent_mobile" clearable></el-input>
			</el-form-item>
			<el-form-item label="学生VPS账号" prop="student_vps">
				<el-input v-model="form.student_vps" clearable></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import { pinyin } from 'pinyin-pro'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					name: "",
					mnemonic_code: "",
					mobile: "",
					sex: 0,
					school_code: "",
					birthday:"",
					parent_mobile:"",
					student_vps:"",
					status:1
				},
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入学生姓名'}
					],
					mnemonic_code: [
						{required: true, message: '请输入学生姓名'}
					],
					mobile: [
						{required: true, message: '请输入学生电话'}
					],
					parent_mobile: [
						{required: true, message: '请输入家长电话'}
					],
					sex: [
						{required: true, message: '请选择性别'}
					],
					school_code: [
						{required: true, message: '请选择校区'}
					],
					status: [
						{required: true, message: '请选择状态'}
					],
				},
				schools:[]
			}
		},
		computed:{
			
		},
		mounted() {
			this.getAllSchool()
		},
		methods: {
			//显示
			open(mode){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = [];
						if (this.mode=='add') {
							res = await this.$API.teach.student.add.post(this.form);
						}
						if (this.mode=='edit') {
							res = await this.$API.teach.student.edit.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.name = data.name
				this.form.mnemonic_code = data.mnemonic_code
				this.form.mobile = data.mobile
				this.form.sex = data.sex
				this.form.school_code = data.school_code
				this.form.birthday = data.birthday
				this.form.parent_mobile = data.parent_mobile
				this.form.student_vps = data.student_vps,
				this.form.status = data.status
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			},
			async getAllSchool(){
				var res = await this.$API.common.getAllSchool.get();
				if(res.code == 1){
					this.schools = res.data
				}
			},
			get_mnemonic_code(){
				this.form.mnemonic_code = pinyin(this.form.name, {
					pattern: 'first',
					toneType: 'none',
					type: 'array',
				}).join('').toUpperCase()
			}
		}
	}
</script>

<style>
</style>
